<?php
/**
 * Created by PhpStorm.
 * User: cyc
 * Date: 2017/11/1
 * Time: 下午2:53
 */

class ApiSeriesService extends ApiBaseService
{

    /**
     * @param $params
     *  page  | false    |int     |分页,默认为0
     * size| false    |int     |偏移量，默认为4
     */
    public static function allAction($params)
    {
        $page = self::util_array_get($params, 'page', 0);
        $size = self::util_array_get($params, 'size', 4);
        $serieses = array();
        $cdbcriteria = new CDbCriteria();
        $cdbcriteria->offset = $page;
        $cdbcriteria->limit = $size;

        foreach (Series::model()->findAll($cdbcriteria) as $series) {
            $serieses[] = ['id' => $series->id, 'name' => $series->name, 'color' => $series->color,];
        }

        return $serieses;
    }


//param | required | type   |describe
//----  |------    |------  |----
//sid  | true    |int     |分类id
//color  | false    |int     |是否返回描述的颜色默认为1 返回，可选值0 不返回
//page  | false    |int     |分页,默认为0
//size | false    |int     |分页大小，默认为3个

//{"id":1,"name":"每日大法好","avater":"http://tv.libooc.com/avater/topic/xxx.jpg","color":"#ffff"},


    public function topicsAction($params)
    {
        $sid = self::util_array_get($params, 'sid');
        $page = self::util_array_get($params, 'page', 0);
        $size = self::util_array_get($params, 'size', 3);
        if ($series = Series::model()->with()->findByPk($sid)) {
            $topics = [];
            $criteria = new CDbCriteria();
            $criteria->addInCondition('id',array_map(function (Topic $topic){
                return $topic->id;
            } ,$series->topics));
            $criteria->limit = $size;
            $criteria->offset = $page;
            foreach (Topic::model()->findAll($criteria) as $topic)
            {
                $topics[] =['id' => $topic->id, 'name' => $topic->name,'avater'=>$topic->getRealUrl(), 'color' => $series->color,];
            }
            return $topics;
        }
        return [];
    }

}